<template>
  <div class="home">
    <PageHeader :tabName="tabName"></PageHeader>
    <div class="homeContent">
      <div style=" width: 300px;">
        <ul>
          <li v-for="(item, i) in tabList" :key="item" @click="changIndex(i, item.tabName)"
            :class="i == tabIndex ? 'active' : ''">
            {{ item.tabName }}
          </li>
        </ul>
      </div>
      <div class="homeBrief" v-loading="loading">
        <h1>铁岭市红十字会{{ formList.title }}</h1>
        <div style=" display: flex;align-items: center;justify-content: space-between;">
          <p style="font-size: 14px;">
            <span>发布时间：{{ formList.createTime ? formList.createTime.substring(0, 10) : '' }}</span> &nbsp;
            <span>来源：{{ formList.authorName }}</span>
          </p>
          <el-button v-clipboard:copy="dialogImageUrl" v-clipboard:success="copySuccess">分享</el-button>
        </div>
        <div v-html="formList.content"></div>
        <div style="padding-top: 10px ">
          <div v-for="(item, i) in attachList" :key="i" style="padding:10px;text-align: left;">
            <span @click="lookBig(item.url)" class="looking">
              <el-icon>
                <Tickets />
              </el-icon>{{ item.name }}
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name='OrganIzation'>
import { Tickets } from '@element-plus/icons-vue'
import PageHeader from '../components/PageHeader.vue'
import { ElMessage } from 'element-plus'
import { GetArticlecategory, GetArticleList, UploadSave } from '@/api/common'
import { onMounted, ref, onUnmounted } from 'vue';
const tabIndex = ref(1)
const dialogImageUrl = ref(window.location.href)
const tabName = ref('组织简介')
const formList = ref({})
const localUrl = ref('http://192.168.0.202:80')
const tabList = ref([
  { tabName: "组织机构" },
  { tabName: "组织简介" },
  { tabName: "领导机构" },
  { tabName: "机构设置" },
])
const loading = ref(true)
const attachList = ref([])
const changIndex = (index, name) => {
  if (index != 0) {
    tabIndex.value = index
    tabName.value = name
    localStorage.setItem('index', tabIndex.value)
    localStorage.setItem('name', tabName.value)
    getID()
  }
}
const getList = (val) => {
  formList.value = {}
  GetArticleList({ categoryId: val }).then((res) => {
    if (res.data.data.result.length != 0) {
      formList.value = res.data.data.result[0]
      if (formList.value) {
        lookAttaches(formList.value)
      }
    }
  })
  loading.value = false
}
const getID = () => {
  loading.value = true
  GetArticlecategory({ name: tabName.value }).then((res) => {
    const id = res.data.data.result[0].categoryId
    getList(id)
  })
}
const lookAttaches = (val) => {
  attachList.value = []
  const params = {
    fieldName: tabName.value
  }
  params.bussinessId = val.cid
  UploadSave(params).then((res) => {
    res.data.data.map((item) => {
      attachList.value.unshift({ name: item.realName, url: localUrl.value + item.accessUrl })
    })
  })
}
const copySuccess = () => {
  ElMessage({
    message: '分享链接已复制',
    type: 'success',
  })
}
const lookBig = (url) => {
  window.open(url)
}
onMounted(() => {
  const num = localStorage.getItem('index')
  const numName = localStorage.getItem('name')
  if (num) {
    tabIndex.value = num
    tabName.value = numName
  }
  getID()
})
onUnmounted(() => {
  localStorage.removeItem('index')
  localStorage.removeItem('name')
})
</script>

<style lang="scss" scoped>
.home {
  margin: 0 auto;
  width: 85%;
  min-height: calc(100vh - 521px);
  margin-bottom: 20px;

  .homeContent {
    width: 100%;
    display: flex;
    justify-content: space-between;

    ul li:first-child {
      background-image: url(../assets/tab.jpg);
      background-repeat: no-repeat;
      color: #fff;
      font-weight: bolder;
      background-size: 100% 60px;
    }

    .active {
      color: red;
    }

    ul li:not(:first-child):hover {
      color: red;
    }

    ul {
      list-style: none;
      margin: 0;
      padding: 0;

      li {
        height: 60px;
        line-height: 60px;
        text-align: center;
        cursor: pointer;
        font-size: 16px;
      }
    }
  }

  .homeBrief {
    flex: 1;
    overflow: hidden;

    h1 {
      font-size: 28px;
      color: #F56C6C;
      margin: 0;
      padding: 20px 0 40px 0;
      border-bottom: 1px solid #dedcdc;
      text-align: center;
    }
  }
}

.looking {
  cursor: pointer;
  font-size: 12px;
  display: flex;
  align-items: center;
}

.looking:hover {
  color: red;
}
</style>